package com.mapeditor.graph 
{
	import com.mapeditor.core.Item;
	import flash.display.Sprite;
	import flash.text.TextField;
	import mx.controls.TextInput;
	/**
	 * ...
	 * @author SE
	 */
	public class Menu extends Sprite
	{
		public var infoText:TextField;
		public var value:Array;
		public var current:uint;
		public var item:Item;
		
		public static const LIFE:uint = 0;
		public static const FREQ:uint = 1;
		public static const DIST:uint = 2;
		public static const SPEED:uint = 3;
		public static const POWERUP_TIRO:uint = 4;
		public static const POWERUP_LIFE:uint = 5;
		
		private static var instance:Menu;  
		
		public static function getInstance():Menu
		{
			if (instance == null)  
				instance = new Menu();  				
			return instance;  
		}
		
		function Menu() 
		{
			value = new Array();
			value[LIFE] = 100;
			value[FREQ] = 0.5;
			value[DIST] = 1000;
			value[SPEED] = 200;
			value[POWERUP_LIFE] = 20;
			value[POWERUP_TIRO] = 2;
			
			this.infoText = new TextField();
			infoText.height = 20;
			addChild(infoText);
			current = DIST;
			
		}
		public function nextOption():void
		{
			this.current++;
			if (this.current > 5)
				this.current = 0;
			
			this.checkOption();
			
			show();
		}
		private function checkOption():void 
		{
			var flag:Boolean = true;
			switch(item.type)
			{
				case Item.TOWER:
					while (flag)
					{
						if (this.current > 5)
							this.current = 0;
						
						if (this.current != POWERUP_TIRO && this.current != POWERUP_LIFE && this.current != SPEED)
							flag = false;						
						else					
							this.current++;
					}
					break;
				case Item.SHIP:
					while (flag)
					{
						if (this.current > 5)
							this.current = 0;
						
						if (this.current != POWERUP_TIRO && this.current != POWERUP_LIFE)
							flag = false;						
						else					
							this.current++;
					}
					break;
				case Item.POWERUP:
					while (flag)
					{
						if (this.current > 5)
							this.current = 0;
						
						if (this.current == POWERUP_TIRO || this.current == POWERUP_LIFE)
							flag = false;						
						else					
							this.current++;
					}
					break;
			}
		}
		
		public function setValues(item:Item):void {
			this.item = item;
			
			value[LIFE] = item.life;
			value[FREQ] = item.frequence;
			value[DIST] = item.dist;
			value[SPEED] = item.speed;
			value[POWERUP_TIRO] = item.powerup_tiro;
			value[POWERUP_LIFE] = item.powerup_life;
			
			checkOption();
			
			this.show();
		}
		public function show():void {
			if (!this.contains(infoText))
				this.addChild(infoText);
			
			switch(current)
			{
				case LIFE: showLife(); break;
				case FREQ: showFreq(); break;
				case DIST: showDist(); break;
				case SPEED: showSpeed(); break;
				case POWERUP_TIRO: showPowerUpTiro(); break;
				case POWERUP_LIFE: showPowerUpLife(); break;
			}
		}
		public function clear():void 
		{
			if (this.contains(infoText))
				this.removeChild(infoText);
			this.graphics.clear();
		}
		
		private function setPrecision(number:Number, precision:int):Number 
		{
			precision = Math.pow(10, precision);
			return (Math.round(number * precision)/precision);
		}
		
		public function changeValue(up:Boolean):void {
			switch(current)
			{
				case LIFE: 	
					item.life += up?10:-10;
					this.value[LIFE] = item.life 		
					break;
				case FREQ: 	
					item.frequence += up?0.1: -0.1;	
					item.frequence = setPrecision(item.frequence,1);
					this.value[FREQ] = item.frequence;
					break;
				case DIST: 	
					item.dist += up?100: -100;
					this.value[DIST] = item.dist;
					break;
				case SPEED: 
					item.speed +=up?10: -10;
					this.value[SPEED] = item.speed;	
					break;
				case POWERUP_TIRO: 
					item.powerup_tiro += up?1: -1;
					if (item.powerup_tiro > 3 || item.powerup_tiro < 0)
						item.powerup_tiro = 2;
					this.value[POWERUP_TIRO] = item.powerup_tiro;	
					break;
				case POWERUP_LIFE: 
					item.powerup_life += up?10: -10;
					if (item.powerup_life > 100 || item.powerup_life < 0)
						item.powerup_life = 10;
					this.value[POWERUP_LIFE] = item.powerup_life;						
					break;
			}
			show();
		}
		
		public function showLife():void
		{
			graphics.clear();
			graphics.beginFill(0x0000ff,0.5);
			graphics.drawRect(-10, -30, value[current], 20);						
			graphics.endFill();
			infoText.text = "Life:" + value[current];
			infoText.x = -10;
			infoText.y = -50;
			
		}
		public function showFreq():void
		{
			graphics.clear();
			graphics.beginFill(0xFF0000,0.5);
			graphics.drawRect(-10, -30, value[current] * 100, 20);						
			graphics.endFill();
			infoText.text = "Frequence:" + value[current];
			infoText.x = -10;
			infoText.y = -50;
		}
		public function showDist():void
		{
			graphics.clear();
			graphics.lineStyle(1, 0xFF0000);
			graphics.drawCircle(0, 0, value[current]/10);	
			infoText.text = "Dist:" + value[current];
			infoText.x = -10;
			infoText.y = -50;
		}
		public function showSpeed():void
		{
			graphics.clear();
			graphics.beginFill(0x00FF00,0.5);
			graphics.drawRect(-10, -30, value[current], 20);						
			graphics.endFill();
			infoText.text = "Speed:" + value[current];
			infoText.x = -10;
			infoText.y = -50;
		}
		public function showPowerUpLife():void
		{
			graphics.clear();
			graphics.beginFill(0x0000FF,0.5);
			graphics.drawRect(-10, -30, value[current], 20);						
			graphics.endFill();
			infoText.text = "Life:" + value[current];
			infoText.x = -10;
			infoText.y = -50;
		}
		public function showPowerUpTiro():void
		{
			graphics.clear();
			infoText.text = "Tiro:" + value[current];
			infoText.x = -10;
			infoText.y = -30;
		}
		
	}

}